//package com.skywolf.chem.common.config;
//
//import com.fasterxml.jackson.core.JsonGenerator;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.BeanDescription;
//import com.fasterxml.jackson.databind.JsonSerializer;
//import com.fasterxml.jackson.databind.SerializationConfig;
//import com.fasterxml.jackson.databind.SerializerProvider;
//import com.fasterxml.jackson.databind.module.SimpleModule;
//import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
//import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
//
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.util.Collection;
//import java.util.Date;
//import java.util.List;
//
///**
// * @Author sj
// * Date on 2020/11/5  15:40
// * @Version 1.0
// */
//@Component
//public class JacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {
//
//    /**
//     * 处理数组类型的null值
//     */
//    public class NullArrayJsonSerializer extends JsonSerializer<Object> {
//
//        @Override
//        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
//            if (value == null) {
//                jsonGenerator.writeStartArray();
//                jsonGenerator.writeEndArray();
//            }
//        }
//    }
//
//    /**
//     * 处理对象类型的null值
//     */
//    public class NullObjectJsonSerializer extends JsonSerializer<Object> {
//        @Override
//        public void serialize(Object value, JsonGenerator jsonGenerator, SerializerProvider provider) throws IOException, JsonProcessingException {
//            if (value == null) {
//                jsonGenerator.writeStartObject();
//                jsonGenerator.writeEndObject();
//            }
//        }
//    }
//
//    /**
//     * 处理字符串类型的null值
//     */
//    public class NullStringJsonSerializer extends JsonSerializer<Object> {
//
//        @Override
//        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
//            jsonGenerator.writeString(StringUtils.EMPTY);
//        }
//    }
//
//    /**
//     * 处理数字类型的null值
//     */
//    public class NullNumberJsonSerializer extends JsonSerializer<Object> {
//
//        @Override
//        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
//            jsonGenerator.writeNumber(0);
//        }
//    }
//
//    /**
//     * 处理布尔类型的null值
//     */
//    public class NullBooleanJsonSerializer extends JsonSerializer<Object> {
//
//        @Override
//        public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
//            jsonGenerator.writeBoolean(false);
//        }
//    }
//
//    /**
//     * 日期序列化实现类
//     */
//    public class DateJsonSerializer extends JsonSerializer<Object> {
//        @Override
//        public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
//            if (value == null) {
//                jgen.writeString(StringUtils.EMPTY);
//            } else {
//                jgen.writeObject(value);
//            }
//        }
//    }
//
//    public class MyBeanSerializerModifier extends BeanSerializerModifier {
//
//
//        @Override
//        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
//            //循环所有的beanPropertyWriter
//            for (Object beanProperty : beanProperties) {
//                BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty;
//                //判断字段的类型，如果是array，list，set则注册nullSerializer
//                if (isArrayType(writer)) {
//                    //给writer注册一个自己的nullSerializer
//                    writer.assignNullSerializer(new NullArrayJsonSerializer());
//                } else if (isNumberType(writer)) {
//                    writer.assignNullSerializer(new NullNumberJsonSerializer());
//                } else if (isBooleanType(writer)) {
//                    writer.assignNullSerializer(new NullBooleanJsonSerializer());
//                } else if (isStringType(writer)) {
//                    writer.assignNullSerializer(new NullStringJsonSerializer());
//                } else if (isDate(writer)) {
//                    writer.assignNullSerializer(new DateJsonSerializer());
//                } else if (isObjectType(writer)) {
//                    writer.assignNullSerializer(new NullObjectJsonSerializer());
//                }
//            }
//            return beanProperties;
//        }
//
//        /**
//         * 是否是数组
//         */
//        private boolean isArrayType(BeanPropertyWriter writer) {
//            Class<?> clazz = writer.getType().getRawClass();
//            return clazz.isArray() || Collection.class.isAssignableFrom(clazz);
//        }
//
//        /**
//         * 是否是string
//         */
//        private boolean isStringType(BeanPropertyWriter writer) {
//            Class<?> clazz = writer.getType().getRawClass();
//            return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);
//        }
//
//
//        /**
//         * 判断日期类型
//         *
//         * @param writer
//         * @return
//         */
//        boolean isDate(BeanPropertyWriter writer) {
//            Class<?> clazz = writer.getType().getRawClass();
//            return clazz.equals(Date.class) || clazz.equals(LocalDateTime.class) || clazz.equals(LocalDate.class);
//        }
//
//        /**
//         * 是否是int
//         */
//        private boolean isNumberType(BeanPropertyWriter writer) {
//            Class<?> clazz = writer.getType().getRawClass();
//            return Number.class.isAssignableFrom(clazz);
//        }
//
//        /**
//         * 是否是boolean
//         */
//        private boolean isBooleanType(BeanPropertyWriter writer) {
//            Class<?> clazz = writer.getType().getRawClass();
//            return clazz.equals(Boolean.class);
//        }
//
//        private boolean isObjectType(BeanPropertyWriter writer) {
//            Class<?> clazz = writer.getType().getRawClass();
//            return clazz instanceof Object;
//        }
//
//    }
//
//    JacksonHttpMessageConverter() {
//        getObjectMapper().setSerializerFactory(getObjectMapper().getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));
//    }
//
//}
